/**
 * COMMON JS
 */

// Name Space for CRM System
// utils
Ext.ns('crm.utils');
// global
Ext.ns('crm.global');
crm.g = crm.global;

// to string
crm.utils.toStr = function(o) {
    var all = '';
    for (var k in o) {
        all += k + ":" + o[k] + '\n';
    }
    return all;
};

// encode
crm.utils.encodeEach = function(o) {
    for (var k  in o) {
        o[k] = Ext.encode(o[k]);
    }
};

// is empty
crm.utils.isEmpty = function(o) {
	if (o === undefined) return true;
	if (o == null) return true;
	if (o == "") return true;
	return false;
};


/**
 * Create Top Tab
 *
 * @param opts Options (id, name, url,qparams)
 */
crm.utils.createTopTab = function(opts) {
	var menuId = "tbp_" + opts.id;
    var tab = Ext.getCmp(menuId);
    if (tab == null) {
    	var qparams = null;
    	if (!crm.utils.isEmpty(opts.qparams)) {
    		qparams = opts.qparams;
    	} else {
    		qparams = {};
    	}
    	qparams.menuId = menuId;
        tab = Ext.create("Ext.panel.Panel", {
            id: menuId,
            title: opts.name,
            closable: true,
            layout: "fit",
            loader: {
                baseParams: qparams,
                autoLoad: true,
                scripts: true,
                url: opts.url
            }
        });
        crm.g.tabs.add(tab).show();
    } else {
    	crm.g.tabs.setActiveTab(tab);
    }
};


/**
 * Show Tab Content
 *
 * @param opts Options (mid(String), items(Object), focusId(String))
 */
crm.utils.showTabContent = function(opts) {
	Ext.getCmp(opts.mid).add({
		xtype: "panel",
	    bodyPadding: 0,
	    border: 0,
	    layout: "border",
	    margin: 0,
	    items: opts.items
	});
	if (!crm.utils.isEmpty(opts.focusId)) {
		Ext.getCmp(opts.focusId).focus();
	}
};

/**
 * Initialize Remote combo box
 *
 */
crm.utils.initRemoteComboBox = function(comboId, value) {
	var store = Ext.getCmp(comboId).getStore();
	store.on("load", function() {
	    Ext.getCmp(comboId).setValue(value);
	});
	store.load();
};

